package com.ui.basic.service.masterdata;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jsecurity.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.common.basic.util.ComboBoxPopulator;
import com.common.basic.util.Globals;
import com.ui.basic.assembler.masterdata.BankAssembler;
import com.ui.basic.command.masterdata.CommandBank;
import com.ui.basic.ibatis.dao.masterdata.TblMBankDAO;
import com.ui.basic.ibatis.dao.masterdata.TblMCountryDAO;
import com.ui.basic.ibatis.model.masterdata.TblMBank;
import com.ui.basic.ibatis.model.masterdata.TblMBankExample;
import com.ui.basic.ibatis.model.masterdata.TblMCountryExample;

@Service("bankService")
public class BankServiceImpl implements BankService {
	private static final transient org.apache.commons.logging.Log LOGGER = org.apache.commons.logging.LogFactory
			.getLog(BankServiceImpl.class);

	@Autowired
	private TblMBankDAO tblMBankDAO;
	@Autowired
	private TblMCountryDAO tblMCountryDAO;

	public void setTblMBankDAO(TblMBankDAO tblMBankDAO) {
		this.tblMBankDAO = tblMBankDAO;
	}

	public void setTblMCountryDAO(TblMCountryDAO tblMCountryDAO) {
		this.tblMCountryDAO = tblMCountryDAO;
	}

	@Transactional(readOnly = true)
	public CommandBank getBank(CommandBank commandBank) throws SQLException {
		LOGGER.debug(">>>BankServiceImpl.getBank");
		if (commandBank != null) {
			TblMBankExample example = new TblMBankExample();

			int rec = 0;
			try {
				rec = tblMBankDAO.countByExample(example);
			} catch (NullPointerException npe) {
				LOGGER.error(npe);
			}
			commandBank.calculate(rec);
			example.setIntRowNum((commandBank.getPgIndex() - 1)
					* commandBank.getPgRows());
			example.setIntRowsPerPage(commandBank.getPgRows());
			example.setOrderByClause("CODE");
			commandBank.setData(tblMBankDAO.selectByExample(example));
		}
		return commandBank;
	}

	@Transactional(readOnly = true)
	public CommandBank getBankDetail(CommandBank commandBank)
			throws SQLException {
		LOGGER.debug(">>>BankServiceImpl.getBankDetail");
		commandBank.setProperty(tblMBankDAO.selectByPrimaryKey(commandBank.getId()));
		return commandBank;
	}

	@Transactional(readOnly = true)
	public void setCombo(CommandBank commandBank) throws SQLException {
		LOGGER.debug(">>>BankServiceImpl.setCombo");
		TblMCountryExample countryExample = new TblMCountryExample();
		countryExample.setOrderByClause("CODE");
		commandBank.setCountryList(ComboBoxPopulator.populate(
				tblMCountryDAO.selectByExample(countryExample), "id",
				new String[] { "code", "name" }, "-"));
	}

	@Transactional
	public void delete(CommandBank commandBank) throws SQLException {
		LOGGER.debug(">>>BankServiceImpl.delete");
		TblMBankExample example = new TblMBankExample();
		
		if(commandBank.getId()!=null){
			example.createCriteria().andIdEqualTo(commandBank.getId());
		}else if(commandBank.getSelectedRecord()!=null && commandBank.getSelectedRecord().length>0){
			List<Long> idList = new ArrayList<Long>();
			for(String strId: commandBank.getSelectedRecord())
				idList.add(Long.valueOf(strId));
			example.createCriteria().andIdIn(idList);
		}
		
		tblMBankDAO.deleteByExample(example);
	}

	@Transactional
	public void save(CommandBank commandBank) throws SQLException {
		LOGGER.debug(">>>BankServiceImpl.save");
		BankAssembler assembler = new BankAssembler();
		TblMBank bank = assembler.toEntity(commandBank);

		if(commandBank.getId() != null){
			bank.setChangedby((String) (SecurityUtils.getSubject().getSession()
					.getAttribute(Globals.CURRENT_USER_NAME)));
			bank.setChangeddate(new Date());
			try {
				tblMBankDAO.updateByPrimaryKeySelective(bank);

			} catch (SQLException e) {
				LOGGER.error("***SQL Code == " + e.getErrorCode());
				LOGGER.error("***error msg == " + e.getMessage());
				throw e;
			}
		}else{
			bank.setCreatedby((String) (SecurityUtils.getSubject().getSession()
					.getAttribute(Globals.CURRENT_USER_NAME)));
			bank.setCreateddate(new Date());
			try {
				tblMBankDAO.insert(bank);
			} catch (SQLException e) {
				LOGGER.error("***SQL Code == " + e.getErrorCode());
				LOGGER.error("***error msg == " + e.getMessage());
				throw e;
			}
		}
	}

}
